package com.kstech.ks_gaz01.data

import com.blankii.androidlibrary.exts.loge
import com.kstech.ks_gaz01.XML_CONFIG
import com.kstech.ks_gaz01.XML_REALTIME
import com.kstech.ks_gaz01.XML_REALTIME_FLOAT
import com.kstech.ks_gaz01.XML_SENSORSET
import com.kstech.ks_gaz01.XML_SIMULATION
import com.kstech.ks_gaz01.XML_VERSION
import com.kstech.ks_gaz01.gz_size

/**
 * author: lenovo
 * created on: 2023/12/18 16:06
 * description: 调试数据表
 */
object DebugDataBean {
    val zt_array = arrayListOf("Min", "Mid1", "Mid2", "Max")
    var xmlMap = HashMap<String, ArrayList<Parent>>()
    lateinit var dataConfig_ZtProgress: ZtProgressObject
    lateinit var xmlSettings: HashMap<String, Boolean>
    lateinit var errorParent: Parent //提前初始化故障信息，使用空间节省时间
    lateinit var dataList_ztProgress: ArrayList<String>
    var floatDataType: String = "left"//RealTimeFloat

    init {
        xmlMap[XML_CONFIG] = ArrayList()
        xmlMap[XML_VERSION] = ArrayList()
        xmlMap[XML_REALTIME] = ArrayList()
        xmlMap[XML_SENSORSET] = ArrayList()
        xmlMap[XML_REALTIME_FLOAT] = ArrayList()
        xmlMap[XML_SIMULATION] = ArrayList()
    }

    fun afterLoadXml() {
        xmlMap[XML_REALTIME]?.forEach {
            if (it.name == "故障信息") {
                errorParent = it
                "故障信息个数:${it.itemList.size}".loge("故障信息")
                gz_size = it.itemList.size
                return
            }
        }
    }

    fun getParentByIndex(titleIndex: Int): Parent {
        return xmlMap[XML_CONFIG]!![titleIndex]
    }

    fun getChildListByIndex(parentIndex: Int): MutableList<Parent.Child> {
        return xmlMap[XML_CONFIG]!![parentIndex].childList
    }

    fun getGroupListByIndex(parentIndex: Int, childIndex: Int): MutableList<Parent.Child.Group> {
        return xmlMap[XML_CONFIG]!![parentIndex].childList[childIndex].groupList
    }

    fun getItemByIndex(titleIndex: Int, itemIndex: Int): Item {
        return xmlMap[XML_CONFIG]!![titleIndex].itemList[itemIndex]
    }

    private fun getItemListFromParent(parentIndex: Int): MutableList<Item> {
        return xmlMap[XML_CONFIG]!![parentIndex].itemList
    }

    private fun getItemListFromChild(parentIndex: Int, childIndex: Int): MutableList<Item> {
        return xmlMap[XML_CONFIG]!![parentIndex].childList[childIndex].itemList
    }

    private fun getItemListFromGroup(
        parentIndex: Int, childIndex: Int, groupIndex: Int
    ): MutableList<Item> {
        return xmlMap[XML_CONFIG]!![parentIndex].childList[childIndex].groupList[groupIndex].itemList
    }

    fun getItemListByIndex(
        parentIndex: Int, childIndex: Int = -1, groupIndex: Int = -1
    ): MutableList<Item> {
        return if (childIndex == -1) {
            getItemListFromParent(parentIndex)
        } else if (groupIndex == -1) {
            getItemListFromChild(parentIndex, childIndex)
        } else {
            getItemListFromGroup(parentIndex, childIndex, groupIndex)
        }
    }


    class Parent {
        lateinit var name: String
        var selected = false// 单选状态
        var isExistedChildTab: Boolean = false
            //是否存在childTabs
            get() = childList.size > 0

        var xmlTag = ""//tag 分类Config RealTime Version 给Parent 进行分类
        lateinit var itemList: MutableList<Item> //所有的数据列表child 已经在初始化加入到里边
        lateinit var childList: MutableList<Child>
        var msgList: MutableList<String>? = null
        var sensorSetModel: String? = null

        class Child {
            lateinit var name: String
            lateinit var itemList: MutableList<Item>
            lateinit var groupList: MutableList<Group>

            //            lateinit var itemsAddress: String
            var isExistedGroupTab: Boolean = false
                //是否存在groupTabs
                get() = groupList.size > 0

            class Group {
                //                lateinit var itemsAddress: String
                lateinit var name: String
                lateinit var itemList: MutableList<Item>
            }
        }
    }

    class Item() {
        constructor(para0: String, para1: String, revData: String) : this() {
            this.para0 = para0
            this.para1 = para1
            this.revData = revData
        }

        constructor(
            para0: String,
            para1: String,
            para5: String,
            isNegative: Boolean,
            bitPosition: Int,
            revData: String
        ) : this() {
            this.para0 = para0
            this.para1 = para1
            this.para5 = para5
            this.bitPosition = bitPosition
            this.isNegativeNumber = isNegative
            this.revData = revData
        }

        lateinit var bitArray: MutableList<String>

        fun isBitArrayInitialized(): Boolean {
            return this::bitArray.isInitialized
        }

        var bitPosition = 0
        var para0: String = "" //参数地址
        var para1: String = "" //标题
        var para2: String = "" //groupAddress
        var para3: String = "" //show
        var para4: String = "" //无用
        var para5: String = "" //单位
        var isNegativeNumber = false // 是否是负数
        var revData: String = "00" //从控制器接收的结果
        var writeRes: Boolean = false//写入结果
        var isCheckBox: Boolean = false//仿真界面的checkbox
    }

    class XmlSetting {
        lateinit var name: String
        var value: Boolean = true
    }
}